<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}

class mhome extends CI_Model {

	function __construct() {
		parent::__construct();
        $ci = & get_instance();
        $ci->load->model('mdealer');		
	}
	
	function getdata($param="", $param1="", $param2="", $param3=""){
		$select = "";
		$from	= "";
		$where	= "WHERE 1=1 ";
		$join	= "";
		
		//if(isset($this->input->post('field'))){
		//}
		
		switch($param){
			// Modul Supply
			case 'searchdealer':
				$select .= "A.id, A.nama_dealer ";
				$from 	.= "tbl_master_dealer";
			break;
			case 'searchmutasi':
				$parameter_data = $this->input->post('parameter_data');
				$select .= "A.no_mutasi_barang, A.id, A.tgl_mutasi, B.nama_karyawan, C.nama_dealer";
				$from 	.= "tbl_supply_mutasi_barang";
				$join	.= " LEFT JOIN tbl_master_karyawan B ON A.id_pengirim = B.id ";
				$join	.= " LEFT JOIN tbl_master_dealer C ON A.id_dealer = C.id ";
				if($parameter_data == 'penerimaan_generator'){
					$where .= " AND A.jns_mutasi_barang = '1' ";
				}elseif($parameter_data == 'penerimaan_promosi'){
					$where .= " AND A.jns_mutasi_barang = '2' ";
				}
			break;
			case 'searchkaryawan':
				$select .= "A.id, A.nama_karyawan";
				$from 	.= "tbl_master_karyawan";
			break;
			case 'searchbarang':
			case 'searchbarang_proc':
				$parameter_data = $this->input->post('parameter_data');
				$select .= "A.id, A.nama_barang";
				$from 	.= "tbl_master_barang";
				if($param == 'searchbarang_proc'){
					$where .= "";
				}else{
					if($parameter_data == 'penerimaan_generator'){
						$where .= " AND A.jenis_barang = '2' ";
					}elseif($parameter_data == 'penerimaan_promosi'){
						$where .= " AND A.jenis_barang = '4' ";
					}
				}
			break;
			case 'searchgudang':
				$select .= "A.id, A.nama_gudang";
				$from 	.= "tbl_master_gudang";
			break;
			//end
			//modul Procurement
			case 'searchkaryawan_proc':

				$select .= "A.*,B.nama_dealer,C.nama_divisi,D.nama_departemen, E.status_karyawan,F.nama_group_dealer ";
				$from 	.= "tbl_master_karyawan";
				$join	.= " 
						LEFT JOIN tbl_master_dealer B ON A.id_dealer=B.id
						LEFT JOIN idx_divisi C ON A.id_divisi=C.id 
						LEFT JOIN idx_departemen D ON A.id_departemen=D.id
						LEFT JOIN idx_status_karyawan E ON A.id_status_karyawan=E.id
						LEFT JOIN idx_group_dealer F ON B.id_group_dealer=F.id  
					";
			break;
			
			case 'searchkaryawan_proc':

				$select .= "A.*,B.nama_dealer,C.nama_divisi,D.nama_departemen, E.status_karyawan,F.nama_group_dealer ";
				$from 	.= "tbl_master_karyawan";
				$join	.= " 
						LEFT JOIN tbl_master_dealer B ON A.id_dealer=B.id
						LEFT JOIN idx_divisi C ON A.id_divisi=C.id 
						LEFT JOIN idx_departemen D ON A.id_departemen=D.id
						LEFT JOIN idx_status_karyawan E ON A.id_status_karyawan=E.id
						LEFT JOIN idx_group_dealer F ON B.id_group_dealer=F.id  
					";
			break;
			
			case 'searchsupplier_proc':
				$select .= "A.*,B.nama_group_supplier,D.nama_provinsi,
								E.nama_kabupaten,F.nama_kecamatan,G.nama_kelurahan ";
				$from 	.= "tbl_master_supplier";
				$join	.= " 
						LEFT JOIN idx_group_supplier B ON A.id_group_supplier=B.id 
						LEFT JOIN tbl_master_wilayah C ON A.id_wilayah=C.id 
						LEFT JOIN idx_wilayah_provinsi D ON C.id_provinsi=D.id 
						LEFT JOIN idx_wilayah_kabupaten E ON C.id_kabupaten=E.id_kabupaten 
						LEFT JOIN idx_wilayah_kecamatan F ON C.id_kecamatan=F.id 
						LEFT JOIN idx_wilayah_kelurahan G ON C.id_kelurahan=G.id
					";
			break;
			
			
			case 'autocomplete':
				$q = strtolower($this->input->post('parameter'));
				if (!$q) return;
				
	//			$list['response'] = 'false';
				$query = $this->db->query("
					SELECT nama_barang, id
					FROM tbl_master_barang
					WHERE nama_barang like '%".$q."%'
				")->result_array();
				$list['suggestions'] = array();
				if($query){
//					$list['response'] = 'true';
					foreach($query as $k=>$v){
						//array_push($list, $v['nama_barang']);
						$list['suggestions'][] = array(
														'value'	=> $v['nama_barang'],
													//	'data'	=> $v['id']
													);
					}
				}
				
				return json_encode($list);
				exit;
			break;
			
			case "searchbarang_na":
				$sql="SELECT A.*,B.nama_group_barang,C.kategori_barang,D.nominal_harga,D.ppn 
				FROM tbl_master_barang A
				LEFT JOIN idx_group_barang B ON A.id_group_barang=B.id
				LEFT JOIN idx_kategori_barang C ON A.id_kategori_barang=C.id
				LEFT JOIN (SELECT * FROM tbl_master_harga_barang WHERE type_harga=2)D ON D.id_barang=A.id";
				return $this->mdealer->get_json($sql);
			break;
			
			case "faktur_proc":
				$sql="SELECT *	FROM tbl_faktur_pembelian ";
				return $this->mdealer->get_json($sql);
			break;
			
			case "pemesanan_proc":
				$sql="SELECT A.id as id_pemesanan_supplier,A.id,A.no_pemesanan,A.tgl_pemesanan,
					B.nama_supplier,G.nama_kabupaten,H.nama_kecamatan,
					I.nama_kelurahan,B.alamat,B.no_telp,B.email,A.pemesanan_via,A.sistem_pengiriman,A.flag,A.kode_supplier,
					CASE WHEN A.flag_div='U' THEN 'Divisi Unit'
					WHEN A.flag_div='S' THEN 'Divisi Spare Part'
					WHEN A.flag_div='M' THEN 'Divisi Marketing'
					ELSE 'Divisi Not Member'
					END AS DIVISI
					FROM  tbl_pemesanan_supplier A 
					LEFT JOIN tbl_master_supplier B ON A.kode_supplier=B.id
					LEFT JOIN idx_group_supplier D ON B.id_group_supplier=D.id
					LEFT JOIN tbl_master_wilayah E ON B.id_wilayah=E.id
					LEFT JOIN idx_wilayah_provinsi F ON E.id_provinsi=F.id
					LEFT JOIN idx_wilayah_kabupaten G ON E.id_kabupaten=G.id_kabupaten
					LEFT JOIN idx_wilayah_kecamatan H ON E.id_kecamatan=H.id
					LEFT JOIN idx_wilayah_kelurahan I ON E.id_kelurahan=I.id
					WHERE A.flag='P'";
				return $this->mdealer->get_json($sql);
			break;
			
		}
		
		$sql = "
				SELECT $select 
				FROM $from A 
				$join 
				$where 
		";
		
		return $this->mdealer->get_json($sql); 		
	}

	
}
